using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class AlbumBackgroundMgr
	{
		private const string TAG = "AlbumBackgroundMgr";
		public static void saveMasterAlbum(AllLanguageMasterMap album)
		{
			GConfigMgr.write<AllLanguageMasterMap>(album);
		}
		public static MasterAlbumList getMasterAlbum()
		{
			AllLanguageMasterMap allLanguageMasterMap = GConfigMgr.read<AllLanguageMasterMap>();
			if (allLanguageMasterMap == null || allLanguageMasterMap.allLanguageMasterMap.get_Count() <= 0)
			{
				GetPackageListMgr.getPackageList(4);
				return null;
			}
			MasterAlbumList masterAlbumList = null;
			allLanguageMasterMap.allLanguageMasterMap.TryGetValue(AlbumBackgroundMgr.getCurrentLanguage(), ref masterAlbumList);
			if (masterAlbumList == null || masterAlbumList.masterAlbumList.get_Count() <= 0)
			{
				GetPackageListMgr.getPackageList(4);
				return null;
			}
			double num = Util.getNowMilliseconds() - masterAlbumList.lastUpdateTime;
			string currentLanguage = AlbumBackgroundMgr.getCurrentLanguage();
			if (!masterAlbumList.language.Equals(currentLanguage))
			{
				GetPackageListMgr.getPackageList(4);
				return null;
			}
			if (num - 51840000000.0 > 0.0)
			{
				GetPackageListMgr.getPackageList(4);
			}
			return masterAlbumList;
		}
		public static string getCurrentLanguage()
		{
			string result = "en";
			if (R.isChineseCN())
			{
				result = "zh_CN";
			}
			else
			{
				if (R.isChineseTW())
				{
					result = "zh_TW";
				}
				else
				{
					if (R.isEnglish())
					{
						result = "en";
					}
					else
					{
						if (R.isGermany())
						{
							result = "de_DE";
						}
					}
				}
			}
			return result;
		}
		public static MasterAlbumList parasXmlToMasterAlbum(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				int num = strXml.IndexOf("<", 0);
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			MasterAlbumList result;
			try
			{
				MasterAlbumList masterAlbumList = null;
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("albumList");
				if (xElement == null)
				{
					result = null;
				}
				else
				{
					if (masterAlbumList == null)
					{
						masterAlbumList = new MasterAlbumList();
					}
					masterAlbumList.language = xElement.Attribute("lang").get_Value();
					IEnumerable<XElement> enumerable = xElement.Elements();
					using (IEnumerator<XElement> enumerator = enumerable.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							XElement current = enumerator.get_Current();
							if (current != null)
							{
								MasterAlbum masterAlbum = new MasterAlbum();
								XElement xElement2 = current.Element("author");
								if (xElement2 != null)
								{
									XElement xElement3 = xElement2.Element("name");
									if (xElement3 != null)
									{
										masterAlbum.authorName = xElement3.get_Value();
									}
									XElement xElement4 = xElement2.Element("title");
									if (xElement4 != null)
									{
										masterAlbum.authorTitle = xElement4.get_Value();
									}
									XElement xElement5 = xElement2.Element("description");
									if (xElement5 != null)
									{
										masterAlbum.authorDescription = xElement5.get_Value();
									}
									XElement xElement6 = xElement2.Element("quote");
									if (xElement6 != null)
									{
										masterAlbum.authorQuote = xElement6.get_Value();
									}
								}
								XElement xElement7 = xElement2.Element("icon");
								if (xElement7 != null)
								{
									XElement xElement8 = xElement7.Element("media");
									if (xElement8 != null)
									{
										if (masterAlbum.authorHeadImg == null)
										{
											masterAlbum.authorHeadImg = new TloMedia();
										}
										XElement xElement9 = xElement8.Element("id");
										if (xElement9 != null)
										{
											masterAlbum.authorHeadImg.nID = ulong.Parse(xElement9.get_Value());
										}
										XElement xElement10 = xElement8.Element("type");
										if (xElement10 != null)
										{
											masterAlbum.authorHeadImg.nType = int.Parse(xElement10.get_Value());
										}
										XElement xElement11 = xElement8.Element("title");
										if (xElement11 != null)
										{
											masterAlbum.authorHeadImg.strTitle = xElement11.get_Value();
										}
										XElement xElement12 = xElement8.Element("description");
										if (xElement12 != null)
										{
											masterAlbum.authorHeadImg.strDescription = xElement12.get_Value();
										}
										XElement xElement13 = xElement8.Element("private");
										if (xElement13 != null)
										{
											masterAlbum.authorHeadImg.nPrivate = int.Parse(xElement13.get_Value());
										}
										XElement xElement14 = xElement8.Element("url");
										if (xElement14 != null)
										{
											masterAlbum.authorHeadImg.strUrl = xElement14.get_Value();
											masterAlbum.authorHeadImg.nUrlType = int.Parse(xElement14.Attribute("type").get_Value());
										}
										XElement xElement15 = xElement8.Element("thumb");
										if (xElement15 != null)
										{
											masterAlbum.authorHeadImg.strThumb = xElement15.get_Value();
											masterAlbum.authorHeadImg.nThumbType = int.Parse(xElement15.Attribute("type").get_Value());
										}
										XElement xElement16 = xElement8.Element("size");
										if (xElement16 != null)
										{
											masterAlbum.authorHeadImg.nWidth = float.Parse(xElement16.Attribute("width").get_Value());
											masterAlbum.authorHeadImg.nHeight = float.Parse(xElement16.Attribute("height").get_Value());
											masterAlbum.authorHeadImg.nTotalSize = float.Parse(xElement16.Attribute("totalSize").get_Value());
										}
									}
								}
								XElement xElement17 = current.Element("groupList");
								if (xElement17 != null)
								{
									using (IEnumerator<XElement> enumerator2 = xElement17.Elements().GetEnumerator())
									{
										while (enumerator2.MoveNext())
										{
											XElement current2 = enumerator2.get_Current();
											if (current2 != null)
											{
												MasterImgGroup masterImgGroup = new MasterImgGroup();
												XElement xElement18 = current2.Element("name");
												masterImgGroup.groupName = xElement18.get_Value();
												XElement xElement19 = current2.Element("mediaList");
												if (xElement19 != null)
												{
													using (IEnumerator<XElement> enumerator3 = xElement19.Elements().GetEnumerator())
													{
														while (enumerator3.MoveNext())
														{
															XElement current3 = enumerator3.get_Current();
															if (current3 != null)
															{
																TloMedia tloMedia = new TloMedia();
																XElement xElement20 = current3.Element("id");
																if (xElement20 != null)
																{
																	tloMedia.nID = ulong.Parse(xElement20.get_Value());
																}
																XElement xElement21 = current3.Element("type");
																if (xElement21 != null)
																{
																	tloMedia.nType = int.Parse(xElement21.get_Value());
																}
																XElement xElement22 = current3.Element("title");
																if (xElement22 != null)
																{
																	tloMedia.strTitle = xElement22.get_Value();
																}
																XElement xElement23 = current3.Element("description");
																if (xElement23 != null)
																{
																	tloMedia.strDescription = xElement23.get_Value();
																}
																XElement xElement24 = current3.Element("private");
																if (xElement24 != null)
																{
																	tloMedia.nPrivate = int.Parse(xElement24.get_Value());
																}
																XElement xElement25 = current3.Element("url");
																if (xElement25 != null)
																{
																	tloMedia.strUrl = xElement25.get_Value();
																	tloMedia.nUrlType = int.Parse(xElement25.Attribute("type").get_Value());
																}
																XElement xElement26 = current3.Element("thumb");
																if (xElement26 != null)
																{
																	tloMedia.strThumb = xElement26.get_Value();
																	tloMedia.nThumbType = int.Parse(xElement26.Attribute("type").get_Value());
																}
																XElement xElement27 = current3.Element("size");
																if (xElement27 != null)
																{
																	tloMedia.nWidth = float.Parse(xElement27.Attribute("width").get_Value());
																	tloMedia.nHeight = float.Parse(xElement27.Attribute("height").get_Value());
																	tloMedia.nTotalSize = float.Parse(xElement27.Attribute("totalSize").get_Value());
																}
																masterImgGroup.imgList.Add(tloMedia);
															}
														}
													}
												}
												masterAlbum.imgGroupList.Add(masterImgGroup);
											}
										}
									}
								}
								masterAlbumList.masterAlbumList.Add(masterAlbum);
							}
						}
					}
					result = masterAlbumList;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("AlbumBackgroundMgr", ex.ToString());
				result = null;
			}
			return result;
		}
	}
}
